//
//  AudioSlide.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/3/13.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

@objc protocol AudioSlideDelegate {
    func audioSlide_didSelectBtn(audioSlide:AudioSlide,senber:ZHButton,index:Int)
}
class AudioSlide: NSView {
    
    let titles = ["电话呼入","电脑音频"]
    
    var subs:[ZHButton] = []
    
    
    
    let kSelectSpacing:CGFloat = 1
    
    weak var delegate:AudioSlideDelegate?
    
    var selectInx:Int = 1 {
        didSet {
            self.displayIfNeeded()
            let width = self.frame.size.width/CGFloat(titles.count)
            self.selectView.snp.updateConstraints { (make) in
                make.left.equalTo(width*CGFloat(selectInx)+kSelectSpacing)
            }
            self.reloadSubs()
        }
    }
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        self.setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        
        self.addSubview(contentView)
        contentView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        self.contentView.zh_border(radius: 13, borderColor: nil, borderWidth: 0)
        
        
        
        let width = (kAudioWindowWidth-50)/CGFloat(titles.count)
        contentView.addSubview(selectView)
        selectView.snp.makeConstraints { (make) in
            make.width.equalToSuperview().multipliedBy(1.0/CGFloat(titles.count)).offset(-kSelectSpacing*2)
            make.centerY.equalToSuperview()
            make.height.equalToSuperview().offset(-(kSelectSpacing*2))
            make.left.equalToSuperview().offset(width*CGFloat(1)+kSelectSpacing)
        }
        self.selectView.zh_border(radius: 12, borderColor: nil, borderWidth: 0)
        
        self.initSubViews()
    }
    
    lazy var contentView: NSView = {
        let view = NSView()
        view.wantsLayer = true
        view.layer?.backgroundColor = color_gray(240).cgColor
        return view
    }()
    
    lazy var selectView: NSView = {
        let view = NSView()
        view.wantsLayer = true
        view.layer?.backgroundColor = NSColor.white.cgColor
        return view
    }()
    
    func initSubViews() {
        for (inx,val) in titles.enumerated() {
            let btn = ZHButton()
            btn.title = val
            btn.font = NSFont.systemFont(ofSize: 13)
            btn.tag = inx+100
            btn.zh_color = color_gray(0)
            btn.zh_backgroundColor = NSColor.clear
            btn.backgroundHoverColor = NSColor.clear
            self.contentView.addSubview(btn)
            subs.append(btn)
            btn.target = self
            btn.action = #selector(btnAction(senber:))
            
            btn.snp.makeConstraints { (make) in
                make.width.equalToSuperview().multipliedBy(1.0/CGFloat(titles.count))
                make.height.equalToSuperview()
                make.centerY.equalToSuperview()
                if inx == 0 {
                    make.left.equalTo(0)
                }else {
                    make.left.equalTo(subs[inx-1].snp.right)
                }
                
            }
        }
        
    }
    func reloadSubs() {
        for (inx,btn) in subs.enumerated() {
            btn.displayIfNeeded()
            btn.zh_color = color_gray(1)
            if inx == self.selectInx {
                btn.zh_color = color_main108
            }
        }
    }
    
    @objc func btnAction(senber:ZHButton)  {
        let inx = senber.tag-100
        self.selectInx = inx
        self.delegate?.audioSlide_didSelectBtn(audioSlide: self, senber: senber,index:inx)
    }
    
    
}
